home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / gpt32src.zip / UNIXPC.TRM < prev    next >
Text File  |  1992-03-25  |  15KB  |  540 lines

  1. /*
  2.  * $Id: unixpc.trm,v 3.26 92/03/24 22:35:46 woo Exp Locker: woo $
  3.  */
  4.  
  5. /* GNUPLOT - unixpc.trm */
  6. /*
  7.  * Copyright (C) 1990, 1991, 1992   
  8.  *
  9.  * Permission to use, copy, and distribute this software and its
  10.  * documentation for any purpose with or without fee is hereby granted, 
  11.  * provided that the above copyright notice appear in all copies and 
  12.  * that both that copyright notice and this permission notice appear 
  13.  * in supporting documentation.
  14.  *
  15.  * Permission to modify the software is granted, but not the right to
  16.  * distribute the modified code.  Modifications are to be distributed 
  17.  * as patches to released version.
  18.  *  
  19.  * This software  is provided "as is" without express or implied warranty.
  20.  * 
  21.  * This file is included by ../term.c.
  22.  *
  23.  * This terminal driver supports:
  24.  *  Unix PC's (ATT 3b1)
  25.  *
  26.  * AUTHORS
  27.  *    John Campbell
  28.  * 
  29.  * send your comments or suggestions to (info-gnuplot@ames.arc.nasa.gov).
  30.  * 
  31.  */
  32.  
  33. /*
  34. >From: John Campbell (...!arizona!naucse!jdc)
  35.  
  36. I originally ported gnuplot to the ATT 3b1 (ATT7300) on 12/4/88, and then
  37. added the minimal code needed to bring it up to 2.0 level on 1/28/90.  The 
  38. 3b1, as I view it, is a 720x300 bitmapped, monochrome display (often people 
  39. don't use the top 12 scan lines and thus the effective size is 720x288).  I 
  40. tried to maximize the size of the graph area, by using these top 12 lines 
  41. (normally reserved) and set up a signal handler to restore them upon exit, 
  42. abort, etc.
  43.  
  44. Line styles were "fudged" (they do not know the aspect ratio).  The same
  45. line style may look different depending upon the slope of the curve.  Due to
  46. this only 4 line styles were implemented.  While more line types are possible,
  47. the current styles were chosen as distinguishable.
  48.  
  49. The 3b1 has 4 "special" rows at the bottom that I could not use in graphics
  50. mode.  It has been suggested that we use these lines for command prompting.
  51. Others have requested that we have a graphics window and a command window.
  52. My experience with gnuplot only includes relatively dumb graphics devices--
  53. hence gnuplot "looks and feels" normal to me the way I implemented it.
  54. I welcome either of these changes from someone else, however.
  55. */
  56.  
  57. #include <sys/window.h>   /* Started with tam.h--too much trouble. */
  58. #include <sys/signal.h>
  59. #include <errno.h>
  60.  
  61. #define uPC_HIGH_BIT    (0x8000)
  62.  
  63. typedef unsigned short Scr_type;
  64. typedef unsigned char Scr_kluge;
  65.  
  66. #define uPC_XMAX 720
  67. #define uPC_YMAX 300
  68.  
  69. #define uPC_XSIZE       45 /* Short ints. */
  70. #define uPC_YSIZE uPC_YMAX
  71.  
  72. Scr_type uPC_display[uPC_YSIZE][uPC_XSIZE];
  73. int uPC_width = 2*uPC_XSIZE;
  74. int uPC_sx=0, uPC_sy=0;
  75. int uPC_cur_linetype=0;
  76. int uPC_angle = 0;
  77. unsigned short uPC_raster_count=0;
  78. static Scr_type lookup[] = {
  79.       0x0001, 0x0002, 0x0004, 0x0008,
  80.       0x0010, 0x0020, 0x0040, 0x0080,
  81.       0x0100, 0x0200, 0x0400, 0x0800,
  82.       0x1000, 0x2000, 0x4000, 0x8000,
  83.    };
  84.  
  85. #define uPC_XLAST (uPC_XMAX - 1)
  86. #define uPC_YLAST (uPC_YMAX - 1)
  87.  
  88. #define uPC_VCHAR FNT5X9_VCHAR
  89. #define uPC_HCHAR FNT5X9_HCHAR
  90. #define uPC_VTIC  uPC_VCHAR/2  /* Was 8  */
  91. #define uPC_HTIC  uPC_HCHAR    /* Was 12 */
  92.  
  93. extern errno, sys_nerr;
  94. extern char *sys_errlist[];
  95.  
  96. static struct urdata uPC_ur = {(unsigned short *)uPC_display, 2*uPC_XSIZE, 0, 0,
  97.                            0, 0, 0, 0, uPC_XMAX, uPC_YMAX, SRCSRC, DSTOR, 0};
  98.  
  99. #define IfErrOut(e1,e2,s1,s2) if (e1 e2) {\
  100. fprintf(stderr, "%s:: %s %s\n", sys_errlist[errno], s1, s2);\
  101. uPC_fixwind(0);\
  102. exit(-1);}
  103.  
  104. uPC_init()
  105. {
  106. /* This routine will ioctl to change 0 size */
  107.    int i;
  108.    struct uwdata uw;
  109.    int uPC_fixwind();
  110.    short gw;
  111.  
  112. /* Check that we are on the bitmapped window. */
  113.    if (iswind() != 0) {
  114.       fprintf (stderr, "Sorry--must run from the bitmapped terminal\n");
  115.       exit(-1);
  116.    }
  117.    for (i=1; i<=16; i++) {
  118.       if (i != SIGINT && i != SIGFPE)  /* Two are caught in plot.c */
  119.          signal (i, uPC_fixwind);
  120.    }
  121.  
  122. /* Increase the screen size */
  123.    uw.uw_x = 0;
  124.    uw.uw_y = 0;   /* Leave room for top status line. */
  125.    uw.uw_width = uPC_XMAX;      /* 720 */
  126.    uw.uw_height = uPC_YMAX;     /* 288 normal--we clobber 12 (top row)*/
  127.    uw.uw_uflags = 1;         /* Creates with no border */
  128.  
  129.    IfErrOut (ioctl(1, WIOCSETD, &uw), <0, "ioctl failed on", "WIOCSETD");
  130. }
  131.  
  132.  
  133. uPC_graphics()
  134. {
  135. /* This routine will clear the uPC_display buffer and window. */
  136.    register Scr_type *j;
  137.    register int i;
  138.  
  139.    j = (Scr_type *)uPC_display;
  140.    i = uPC_YSIZE*uPC_XSIZE + 1;
  141.  
  142.    while (--i)
  143.       *j++ = 0;
  144. /*
  145.    Position the cursor to the bottom of the screen so when we come back to
  146.    text mode we are just below the graph.
  147. */
  148.    printf ("\033[25;1H");
  149.  
  150.    uPC_ur.ur_dstop = DSTSRC;   /* replace (clear screen). */
  151.    IfErrOut (ioctl(1, WIOCRASTOP, &uPC_ur), <0,
  152.       "ioctl failed", "WIOCRASTOP");
  153.    uPC_ur.ur_dstop = DSTOR;   /* Or in (show text) */
  154. }
  155.  
  156.  
  157. uPC_text()
  158. {
  159. /* This routine will flush the display. */
  160.  
  161.    IfErrOut (ioctl(1, WIOCRASTOP, &uPC_ur), <0,
  162.       "ioctl failed", "WIOCRASTOP");
  163. }
  164.  
  165.  
  166. uPC_linetype(linetype)
  167. int linetype;
  168. {
  169. /* This routine records the current linetype. */
  170.    if (uPC_cur_linetype != linetype) {
  171.       uPC_raster_count = 0;
  172.       uPC_cur_linetype = linetype;
  173.    }
  174. }
  175.  
  176.  
  177. uPC_move(x,y)
  178. unsigned int x,y;
  179. {
  180. /* This routine just records x and y in uPC_sx, uPC_sy */
  181.    uPC_sx = x;
  182.    uPC_sy = y;
  183. }
  184.  
  185.  
  186. /* Was just (*(a)|=(b)) */
  187. #define uPC_PLOT(a,b)   (uPC_cur_linetype != 0 ? uPC_plot_word (a,b) :\
  188.                                 (*(a)|=(b)))
  189.  
  190. uPC_plot_word(a,b)
  191. Scr_type *a, b;
  192. /*
  193.    Weak attempt to make line styles.  The real problem is the aspect
  194.    ratio.  This routine is called only when a bit is to be turned on in
  195.    a horizontal word.  A better line style routine would know something
  196.    about the slope of the line around the current point (in order to
  197.    change weighting).
  198.  
  199.    This yields 3 working linetypes plus a usable axis line type.
  200. */
  201. {
  202. /* Various line types */
  203.    switch (uPC_cur_linetype) {
  204.    case -1:
  205.    /* Distinguish between horizontal and vertical axis. */
  206.       if (uPC_sx > uPC_XMAX/8 && uPC_sx < 7*uPC_XMAX/8) {
  207.       /* Fuzzy tolerance because we don't know exactly where the y axis is */
  208.          if (++uPC_raster_count % 2 == 0) *(a) |= b;
  209.       }
  210.       else {
  211.       /* Due to aspect ratio, take every other y pixel and every third x. */
  212.          *(a) |= (b & 0x9999);
  213.       }
  214.    break;
  215.    case 1:
  216.    case 5:
  217.    /* Make a |    |----|    |----| type of line. */
  218.       if ((1<<uPC_raster_count) & 0xF0F0) *(a) |= b;
  219.       if (++uPC_raster_count > 15) uPC_raster_count = 0;
  220.    break;
  221.    case 2:
  222.    case 6:
  223.    /* Make a |----|----|----|--- |    | type of line. */
  224.       if ((1<<uPC_raster_count) & 0x0EFFF) *(a) |= b;
  225.       if (++uPC_raster_count > 19) uPC_raster_count = 0;
  226.    break;
  227.    case 3:
  228.    case 7:
  229.    /* Make a | -  | -  | -  | -  | type of line. */
  230.       if ((1<<uPC_raster_count) & 0x4444) *(a) |= b;
  231.       if (++uPC_raster_count > 15) uPC_raster_count = 0;
  232.    break;
  233.    case 4:
  234.    case 8:
  235.    default:
  236.       *(a) |= b;
  237.    break;
  238.    }
  239. }
  240.  
  241. uPC_vector(x,y)
  242. unsigned int x,y;
  243. {
  244. /* This routine calls line with x,y */
  245.    int x1 = uPC_sx, y1=uPC_sy, x2 = x, y2 = y;
  246.    register int  c, e, dx, dy, width;
  247.    register Scr_type mask, *a;
  248.  
  249. /* Record new sx, sy for next call to the vector routine. */
  250.    uPC_sx = x2;
  251.    uPC_sy = y2;
  252.  
  253.    a = &uPC_display[(uPC_YSIZE - 1) - y1][x1 >> 4];
  254.    mask = lookup[x1 & 0x0f];
  255.    width = uPC_width;
  256.  
  257.    if ((dx = x2 - x1) > 0) {
  258.       if ((dy = y2 - y1) > 0) {
  259.          if (dx > dy) {         /* dx > 0, dy > 0, dx > dy */
  260.             dy <<= 1;
  261.             e = dy - dx;
  262.             c = dx + 2;
  263.             dx <<= 1;
  264.  
  265.             while (--c) {
  266.                uPC_PLOT(a, mask);
  267.                if (e >= 0) {
  268.                   (Scr_kluge *)a -= width;
  269.                   e -= dx;
  270.                }
  271.                if (mask & uPC_HIGH_BIT) {
  272.                   mask = 1;
  273.                   a++;
  274.                } else
  275.                   mask <<= 1;
  276.                e += dy;
  277.             }
  278.          } else {            /* dx > 0, dy > 0, dx <= dy */
  279.             dx <<= 1;
  280.             e = dx - dy;
  281.             c = dy + 2;
  282.             dy <<= 1;
  283.  
  284.             while (--c) {
  285.                uPC_PLOT(a, mask);
  286.                if (e >= 0) {
  287.                   if (mask & uPC_HIGH_BIT) {
  288.                      mask = 1;
  289.                      a++;
  290.                   } else
  291.                      mask <<= 1;
  292.                   e -= dy;
  293.                }
  294.                (Scr_kluge *)a -= width;
  295.                e += dx;
  296.             }
  297.          }
  298.       } else {
  299.          dy = -dy;
  300.          if (dx > dy) {         /* dx > 0, dy <= 0, dx > dy */
  301.             dy <<= 1;
  302.             e = dy - dx;
  303.             c = dx + 2;
  304.             dx <<= 1;
  305.  
  306.             while (--c) {
  307.                uPC_PLOT(a, mask);
  308.                if (e >= 0) {
  309.                   (Scr_kluge *)a += width;
  310.                   e -= dx;
  311.                }
  312.                if (mask & uPC_HIGH_BIT) {
  313.                   mask = 1;
  314.                   a++;
  315.                } else
  316.                   mask <<= 1;
  317.                e += dy;
  318.             }
  319.          } else {            /* dx > 0, dy <= 0, dx <= dy */
  320.             dx <<= 1;
  321.             e = dx - dy;
  322.             c = dy + 2;
  323.             dy <<= 1;
  324.  
  325.             while (--c) {
  326.                uPC_PLOT(a, mask);
  327.                if (e >= 0) {
  328.                   if (mask & uPC_HIGH_BIT) {
  329.                      mask = 1;
  330.                      a++;
  331.                   } else
  332.                      mask <<= 1;
  333.                   e -= dy;
  334.                }
  335.                (Scr_kluge *)a += width;
  336.                e += dx;
  337.             }
  338.          }
  339.       }
  340.    } else {
  341.       dx = -dx;
  342.       if ((dy = y2 - y1) > 0) {
  343.          if (dx > dy) {         /* dx <= 0, dy > 0, dx > dy */
  344.             dy <<= 1;
  345.             e = dy - dx;
  346.             c = dx + 2;
  347.             dx <<= 1;
  348.  
  349.             while (--c) {
  350.                uPC_PLOT(a, mask);
  351.                if (e >= 0) {
  352.                   (Scr_kluge *)a -= width;
  353.                   e -= dx;
  354.                }
  355.                if (mask & 1) {
  356.                   mask = uPC_HIGH_BIT;
  357.                   a--;
  358.                } else
  359.                   mask >>= 1;
  360.                e += dy;
  361.             }
  362.          } else {            /* dx <= 0, dy > 0, dx <= dy */
  363.             dx <<= 1;
  364.             e = dx - dy;
  365.             c = dy + 2;
  366.             dy <<= 1;
  367.  
  368.             while (--c) {
  369.                uPC_PLOT(a, mask);
  370.                if (e >= 0) {
  371.                   if (mask & 1) {
  372.                      mask = uPC_HIGH_BIT;
  373.                      a--;
  374.                   } else
  375.                      mask >>= 1;
  376.                   e -= dy;
  377.                }
  378.                (Scr_kluge *)a -= width;
  379.                e += dx;
  380.             }
  381.          }
  382.       } else {
  383.          dy = -dy;
  384.          if (dx > dy) {         /* dx <= 0, dy <= 0, dx > dy */
  385.             dy <<= 1;
  386.             e = dy - dx;
  387.             c = dx + 2;
  388.             dx <<= 1;
  389.  
  390.             while (--c) {
  391.                uPC_PLOT(a, mask);
  392.                if (e >= 0) {
  393.                   (Scr_kluge *)a += width;
  394.                   e -= dx;
  395.                }
  396.                if (mask & 1) {
  397.                   mask = uPC_HIGH_BIT;
  398.                   a--;
  399.                } else
  400.                   mask >>= 1;
  401.                e += dy;
  402.             }
  403.          } else {            /* dx <= 0, dy <= 0, dx <= dy */
  404.             dx <<= 1;
  405.             e = dx - dy;
  406.             c = dy + 2;
  407.             dy <<= 1;
  408.  
  409.             while (--c) {
  410.                uPC_PLOT(a, mask);
  411.                if (e >= 0) {
  412.                   if (mask & 1) {
  413.                      mask = uPC_HIGH_BIT;
  414.                      a--;
  415.                   } else
  416.                      mask >>= 1;
  417.                   e -= dy;
  418.                }
  419.                (Scr_kluge *)a += width;
  420.                e += dx;
  421.             }
  422.          }
  423.       }
  424.    }
  425. }
  426.  
  427.  
  428. #ifdef uPC_NOT_USED
  429. /* Added by Russell Lang, eln272v@monu1.cc.monash.oz
  430.    This placement to the nearest character cell worked, and I'm leaving
  431.    it here so the calculations involved won't be lost!  (jdc)
  432. */
  433. uPC_put_text(x,y,str)
  434. unsigned int x,y;
  435. char str[];
  436. {
  437. /* This routine puts the text at the cursor location nearest
  438.    to (x,y).  Obviously the exact postion would look better */
  439.  
  440. /* Just use the ANSI escape sequence CUP (iswind said that was ok!) */
  441.    printf ("\033[%d;%dH%s\033[25;1H", (int)(24-(y-uPC_VCHAR/2)*25/uPC_YMAX), 
  442.                 (int)(x*80/uPC_XMAX), str); 
  443.    fflush (stdout);
  444. }
  445. #endif
  446.  
  447.  
  448. uPC_put_text(x,y,str)
  449. unsigned int x,y;
  450. char str[];
  451. {
  452.    if (uPC_angle == 1)
  453.       x += uPC_VCHAR/2;
  454.    else
  455.       y -= uPC_VCHAR/2;
  456.  
  457.    switch (uPC_angle) {
  458.       case 0:
  459.          for (; *str; ++str, x += uPC_HCHAR)
  460.             uPC_putc (x, y, *str, uPC_angle);
  461.       break;
  462.       case 1:
  463.          for (; *str; ++str, y += uPC_HCHAR)
  464.             uPC_putc (x, y, *str, uPC_angle);
  465.       break;
  466.    }
  467. }
  468.  
  469.  
  470. uPC_putc (x, y, c, angle)
  471. unsigned int x, y;
  472. int c, angle;
  473. /*
  474.    Put a character at an x,y location in the bit map (using the fnt5x9
  475.    array.  This is mostly just copied from the bitmap.c driver.
  476. */
  477. {
  478.    int i, j, k;
  479.    register Scr_type mask, *a;
  480.    char_row fc;
  481.    unsigned int pixelon;
  482.  
  483.    i = c - ' ';
  484.    for (j=0; j<FNT5X9_VBITS; j++) {
  485.       fc = fnt5x9[i][j];
  486.       for (k=0; k<FNT5X9_HBITS; k++) {
  487.          pixelon = ((unsigned int)(fc))>>k & 1;
  488.          if (pixelon) {
  489.             switch (angle) {
  490.             case 0:
  491.                mask = lookup[x+k+1 & 0x0f];
  492.                a = &uPC_display[(uPC_YSIZE - 1) - (y+j)][(x+k+1) >> 4];
  493.             break;
  494.             case 1:
  495.                mask = lookup[x-j & 0x0f];
  496.                a = &uPC_display[(uPC_YSIZE - 1) - (y+k+1)][(x-j) >> 4];
  497.             break;
  498.             }
  499.             *(a) |= (mask);  /* see uPC_PLOT macro */
  500.          }
  501.       }
  502.    }
  503. }
  504.  
  505.  
  506. uPC_text_angle (ang)
  507. int ang;
  508. {
  509.    uPC_angle = ang;
  510.    return TRUE;
  511. }
  512.  
  513.  
  514. uPC_reset()
  515. {
  516. /* Reset window to normal size. */
  517.    uPC_fixwind (0);
  518. }
  519.  
  520.  
  521.  
  522. uPC_fixwind(signo)
  523. int signo;
  524. {
  525.    static struct uwdata wreset = { 0, 12, 720, 288, 0x1};
  526.    struct utdata ut;
  527.  
  528. /* Reset the window to the right size. */
  529.    ioctl(1, WIOCSETD, &wreset);   /* 0, not wncur here! */
  530.  
  531. /* Scroll the screen once. (avoids typing over the same line) */
  532.    fprintf (stderr, "\n");
  533.  
  534.    if (signo) {
  535.       if (signo == SIGILL || signo == SIGTRAP || signo == SIGPWR)
  536.          signal (signo, SIG_DFL);
  537.       kill (0,signo);  /* Redo the signal (as if we never trapped it). */
  538.    }
  539. }
  540.